# -*- encoding: utf-8 -*-
"""
@author: acedar  
@time: 2025/3/30 17:41
@file: weather.py 
"""

import json
import httpx
from typing import Any
from mcp.server.fastmcp import FastMCP
from dotenv import load_dotenv
import os
import requests
load_dotenv()
mcp = FastMCP("weather")

# OpenWeather API 配置
# OPENWEATHER_API_BASE = "https://api.openweathermap.org/data/2.5/weather"
# API_KEY = "6fa113425c87105a796b21c51aaac9ad"  # 请替换为你自己的 OpenWeather API Key


API_KEY = os.environ.get("WEATHER_API_KEY")
API_BASE_URL = os.environ.get("WEATHER_BASE_URL")

"735d354dc52e4e67843132841252803"
API_URL = "http://api.weatherapi.com/v1/current.json"

def query_weather(city: str) -> dict[str, Any] | None:
    params = {
        "q": city,
        "key": API_KEY,
        "aqi": "no"  # 关闭空气质量输入
    }
    try:
        response = requests.get(API_BASE_URL, params=params)
        return response.json()  # 返回字典类型
    except Exception as e:
        return {"error": f"请求失败: {str(e)}"}

if __name__ == "__main__":
    resp = query_weather("Shenzhen")
    print("resp:", resp)
